<?php
// +----------------------------------------------------------------------
// | 专利和商标订单管理
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// |2016-1-15 上午11:51:29
// +----------------------------------------------------------------------
defined('BASEPATH') OR exit('No direct script access allowed');

class Order extends MY_Controller{
	
	public function index(){
		$this->load->model('Patent_order_model','order');
		$where = array();
		$total = $this->order->getCount('');//获取数据记录数
		//分页
		$this->load->library('pagination');//载入分页类
		$page_size = 10;//每页显示的记录数
		$url_segment = $this->uri->segment(3);//获取url中的第三段为当前页码
		//获取分页配置
		$suffix = empty( $suffix ) ? '' : $suffix;
		$config = get_page_config(site_url('order/index'),$total, $page_size, 3, $suffix, site_url('/order/index/1/').$suffix);
		$this->pagination->initialize($config);
		$field="*";
		$offset = intval( $url_segment );//获取当前页码
		//获取偏移数，如果当前页码大于0，用当前页码－1*每页记录数
		$offset = $offset > 0 ? ( $offset - 1  ) * $page_size : $offset;
		$list = $this->order->getDataObj($where, $field, $page_size, $offset);//获取数据
		//获取专利商品标题
		$this->load->model('Patent_model', 'patent');
		foreach ($list as $key => $val){
			$patent_title = $this->patent->getFind( array('id'=>$val->patent_type), 'title' );
			$list[$key]->title = $patent_title->title;
		}
		
		$pages = $this->pagination->create_links();//显示分页
		
		$this->load->vars('meta_title', '专利订单');
		$this->load->vars('menu_type', 1);
		$this->load->vars('record_count', $total);
		$this->load->vars('list', $list);
		$this->load->vars('pages', $pages);
		$this->load->view('order/index');
	}
	
	/**
	 * 获取订单联系人
	 */
	public function getContacts(){
		$order_id = intval( $this->input->post('order_id', 0) );
		if ( $order_id > 0 ){
			$this->load->model('Patent_order_contacts_model', 'contacts');
			$info = $this->contacts->getFind( array('pid'=>$order_id) );
			if ( !empty( $info ) ){
				ajaxreturn(1, $info);
			}else{
				ajaxreturn(0, '暂无数据');
			}
		}else{
			ajaxreturn(0,'参数错误');
		}
	}
	
	/**
	 * 设置专利订单状态
	 */
	public function setPatentOrderStatus(){
		$order_id = intval( $this->uri->segment(3, 0) );
		if ( IS_POST ){
			$this->load->model('Patent_order_model', 'order');
			$status = $this->order->updateData( array('id'=>$order_id) );
			if ( $status ){
				ajaxreturn(1, '操作成功', site_url('order/index'));
			}else{
				ajaxreturn(0, '操作失败');
			}
		}else{
			$success  = $order_id > 0 ? 1: 0;
			$this->load->model('Patent_order_model', 'order');
			$info = $this->order->getFind( array('id'=>$order_id), 'id,status,instro' );
			$this->load->vars('success', $success);
			$this->load->vars('info', $info);
			$this->load->view('order/setpatentorderstatus');
		}	
	}
	
	/**
	 * 商标订单列表
	 */
	public function trademark(){
		
		$this->load->model('Trademark_order_model','order');
		$where = array();
		$total = $this->order->getCount('');//获取数据记录数
		//分页
		$this->load->library('pagination');//载入分页类
		$page_size = 10;//每页显示的记录数
		$url_segment = $this->uri->segment(3);//获取url中的第三段为当前页码
		//获取分页配置
		$suffix = empty( $suffix ) ? '' : $suffix;
		$config = get_page_config(site_url('order/index'),$total, $page_size, 3, $suffix, site_url('/order/index/1/').$suffix);
		$this->pagination->initialize($config);
		$field="*";
		$offset = intval( $url_segment );//获取当前页码
		//获取偏移数，如果当前页码大于0，用当前页码－1*每页记录数
		$offset = $offset > 0 ? ( $offset - 1  ) * $page_size : $offset;
		$list = $this->order->getDataObj($where, $field, $page_size, $offset);//获取数据
		//获取专利商品标题
		$this->load->model('Trademark_attr_model', 'trademark');
		foreach ($list as $key => $val){
			$patent_title = $this->trademark->getFind( array('id'=>$val->trademark_id), 'title' );
			$list[$key]->title = $patent_title->title;
		}
		
		$pages = $this->pagination->create_links();//显示分页
		
		$this->load->vars('record_count', $total);
		$this->load->vars('list', $list);
		$this->load->vars('pages', $pages);
		
		$this->load->vars('meta_title', '商标订单');
		$this->load->vars('menu_type', 2);
		$this->load->view('order/trademark');
	}
	
	/**
	 * 获取商标订单联系人
	 */
	public function getTrademarkContacts(){
		$order_id = intval( $this->input->post('order_id', 0) );
		if ( $order_id > 0 ){
			$this->load->model('Trademark_order_contacts_model', 'contacts');
			$info = $this->contacts->getFind( array('pid'=>$order_id) );
			if ( !empty( $info ) ){
				ajaxreturn(1, $info);
			}else{
				ajaxreturn(0, '暂无数据');
			}
		}else{
			ajaxreturn(0,'参数错误');
		}		
	}
	
	/**
	 * 设置专利订单状态
	 */
	public function setTrademarkOrderStatus(){
		$order_id = intval( $this->uri->segment(3, 0) );
		if ( IS_POST ){
			$this->load->model('Trademark_order_model', 'order');
			$status = $this->order->updateData( array('id'=>$order_id) );
			if ( $status ){
				ajaxreturn(1, '操作成功', site_url('order/trademark'));
			}else{
				ajaxreturn(0, '操作失败');
			}
		}else{
			$success  = $order_id > 0 ? 1: 0;
			$this->load->model('Trademark_order_model', 'order');
			$info = $this->order->getFind( array('id'=>$order_id), 'id,status,instro' );
			$this->load->vars('success', $success);
			$this->load->vars('info', $info);
			$this->load->view('order/settrademarkorderstatus');
		}
	}
}